package com.sam.demo.spring.boot.jpa.config;

import com.atomikos.jdbc.AtomikosDataSourceBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;


/**
 * 指定数据源的Repository路径
 * 数据源的entityManagerFactory
 * 事务是公共事务
 * @author Administrator
 *
 */
@EnableJpaRepositories(basePackages = "com.sam.demo.spring.boot.jpa.primary", entityManagerFactoryRef = "primaryEntityManagerFactory", transactionManagerRef = "transactionManager")
@Configuration
@ConfigurationProperties(prefix = "spring.datasource.primary")
@EnableTransactionManagement(proxyTargetClass = true)
public class PrimaryJpaConfig extends JpaConfigCommon {

	@Bean(name = "primaryDataSource")
	@Primary
	public DataSource dataSource() {
		return super.dataSource("primary");
	}

	@Bean(name = "primaryEntityManagerFactory")
	@DependsOn({ "atomikosJtaPlatfom" })
	public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
		return super.entityManagerFactory("primary","com.sam.demo.spring.boot.jpa.primary");

	}
}
